//
//  RankingListView.m
//  tcpoctest
//
//  Created by gump on 6/6/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "RankingListView.h"

#import "sendRLRichDay.h"
#import "sendRLRichWeek.h"
#import "sendRLRichMonth.h"
#import "sendRLRRichQuarter.h"
#import "sendRLRRichYear.h"
#import "sendRLGlamourDay.h"
#import "sendRLGlamourWeek.h"
#import "sendRLGlamourMonth.h"
#import "sendRLGlamourQuarter.h"
#import "sendRLGlamourYear.h"

#import "RankListDataSource.h"
#import "rankListInfo.h"
#import "RanklistCell.h"

@implementation RankingListView

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = [self getTitleName];
    
    self.segmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, TOPBAR_HIGHT, 320, 44)];
    [self.segmentedControl setSectionTitles:@[@"富豪榜", @"魅力榜"]];
    [self.segmentedControl setSelectedSegmentIndex:getRankListKindFilterType() - 1];
    [self.segmentedControl setBackgroundColor:[UIColor colorWithRed:68/255.0 green:68/255.0 blue:68/255.0 alpha:1]];
    [self.segmentedControl setTextColor:[UIColor whiteColor]];
    [self.segmentedControl setSelectionIndicatorColor:
     [UIColor colorWithRed:77/255.0 green:131/255.0 blue:220/255.0 alpha:1]];
    
    __block RankingListView *blockSelf = self;
    
    [self.segmentedControl setIndexChangeBlock:^(NSInteger index) {
        [blockSelf.scrollView scrollRectToVisible:CGRectMake(320 * index, 0, 320, 200) animated:YES];
    }];
    [self.segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentedControl];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, TOPBAR_HIGHT + 44, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20 - 44)];
    [self.scrollView setBackgroundColor:[UIColor colorWithRed:0.1 green:0.2 blue:0.34 alpha:1]];
    [self.scrollView setPagingEnabled:NO];
    self.scrollView.scrollEnabled = NO;
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    [self.scrollView setContentSize:CGSizeMake(WINDOW_WIDTH * 2, WINDOW_HIGTH - TOPBAR_HIGHT - 20 - 44)];
    [self.scrollView scrollRectToVisible:CGRectMake(0, 0, 320, WINDOW_HIGTH - TOPBAR_HIGHT - 20 - 44) animated:NO];
    [self.scrollView setDelegate:self];
    [self.view addSubview:self.scrollView];
    
    richTablev =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20 - 44)
                                                  style:UITableViewStylePlain];
    richTablev.backgroundColor = CELL_BG_COLOR;
    richTablev.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    richTablev.delegate = self;
    richTablev.dataSource = self;
    [self.scrollView addSubview:richTablev];
    
    glamourTablev =[[UITableView alloc]initWithFrame:CGRectMake(WINDOW_WIDTH, 0, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20 - 44)
                                                  style:UITableViewStylePlain];
    glamourTablev.backgroundColor = CELL_BG_COLOR;
    glamourTablev.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    glamourTablev.delegate = self;
    glamourTablev.dataSource = self;
    [self.scrollView addSubview:glamourTablev];
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    UIBarButtonItem *nextItem = [self createBarButtonItemWithTitle:[self getFilterName]
                                           target:self
                                           action:@selector(nextButtonPressed)];
    self.navItem.rightBarButtonItem = nextItem;
}

#pragma mark -private fun-

-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)refreshView
{
    [self.segmentedControl setSelectedSegmentIndex:getRankListKindFilterType() - 1];
    [richTablev reloadData];
    [glamourTablev reloadData];
}

-(void)viewWillShow
{
    [self SendRanklistRequest];
    
    [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在更新..."
                                                      withView:self.view];
    
    iTimeCount = 0;
    iGameState = VGS_START;
    
    [self refreshView];
}

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
	setRankListKindFilterType(segmentedControl.selectedSegmentIndex + 1);
    
    [self SendRanklistRequest];
    
    [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在更新..."
                                                      withView:self.view];
    
    iTimeCount = 0;
    iGameState = VGS_START;
}

-(NSString *)getTitleName
{
    NSString *strname = @"每日排行";
    
    switch (getRankListDataFilterType()) {
        case RLI_RICH_DAY:
            strname = @"每日排行";
            break;
        case RLI_RICH_WEEK:
            strname = @"每月排行";
            break;
        case RLI_RICH_MONTH:
            strname = @"年度排行";
            break;
        default:
            break;
    }
    
    return strname;
}

-(NSString *)getFilterName
{
    NSString *strname = @"筛选(日)";
    
    switch (getRankListDataFilterType()) {
        case RLI_RICH_DAY:
            strname = @"筛选(日)";
            break;
        case RLI_RICH_WEEK:
            strname = @"筛选(月)";
            break;
        case RLI_RICH_MONTH:
            strname = @"筛选(年)";
            break;
        default:
            break;
    }
    
    return strname;
}

-(void)nextButtonPressed
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择排行"
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"每日排行",
                                  @"每月排行",@"年度排行", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    actionSheet.tag = 1;
    [actionSheet showInView:self.view];
}

-(void)UICustomTick
{
    switch (iGameState) {
        case VGS_START:
        {
            if (iTimeCount < 150) {
                iTimeCount++;
            }
            else
            {
                iTimeCount = 0;
                iGameState = VGS_NULL;
                
                [[ViewManager sharedViewManager] dismissProgressHUD];
                
                [[DataControl shareControl].ranklistDS delAllArr];
                
                [self refreshView];
            }
        }
            break;
            
        default:
            break;
    }
}

-(void)SendRanklistRequest
{
    switch (getRankListKindFilterType()) {
        case RLI_RICH_DAY:
        {
            switch (getRankListDataFilterType()) {
                case RLI_RICH_DAY:
                {
                    [[netManager sharedNetManager].sendRLRDay SendRLRDay];
                }
                    break;
                case RLI_RICH_WEEK:
                {
                    [[netManager sharedNetManager].sendRLRMonth SendRLRMonth];
                }
                    break;
                case RLI_RICH_MONTH:
                {
                    [[netManager sharedNetManager].sendRLRYear SendRLRYear];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case RLI_RICH_WEEK:
        {
            switch (getRankListDataFilterType()) {
                case RLI_RICH_DAY:
                {
                    [[netManager sharedNetManager].sendRLGDay SendRLGDay];
                }
                    break;
                case RLI_RICH_WEEK:
                {
                    [[netManager sharedNetManager].sendRLGMonth SendRLGMonth];
                }
                    break;
                case RLI_RICH_MONTH:
                {
                    [[netManager sharedNetManager].sendRLGYear SendRLGYear];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
}

-(void)ReceiveRanklistRequest
{
    if (iGameState == VGS_START) {
        [[ViewManager sharedViewManager] dismissProgressHUD];
        
        iTimeCount = 0;
        iGameState = VGS_NULL;
        
        [self refreshView];
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    CGFloat pageWidth = scrollView.frame.size.width;
//    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
//    [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
//    
//    if (page + 1 != getRankListKindFilterType()) {
//        setRankListKindFilterType(page + 1);
//        [self SendRanklistRequest];
//        
//        [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在更新..."
//                                                          withView:self.view];
//        
//        iTimeCount = 0;
//        iGameState = VGS_START;
//    }
    
    
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [DataControl shareControl].ranklistDS.ArrCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row = [indexPath row];
    
    static NSString *CellIdentifier = @"Cell";
    RanklistCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[RanklistCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        
    switch (section)
    {
        case 0:
        {
            FirendInfo  *fi;
            NSString *url;
            
            rankListInfo *rlinfo = [[DataControl shareControl].ranklistDS RankListInfoByRow:row];
            
            if (rlinfo != nil) {
                fi = [[DataControl shareControl] SearchInfo: rlinfo.strmail];
                url = [[DataControl shareControl] SearchHeadImage:rlinfo.strmail];
                
                
                if (fi != nil)
                {
                    cell.nameLabel.text = fi.name;
                    if (fi.sex == 1) {
                        cell.sexicon.image = [UIImage imageNamed:@"icon_male.png"];
                    }else{
                        cell.sexicon.image = [UIImage imageNamed:@"icon_female.png"];
                    }
                    if ([url length] > 0)
                    {
                        cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                        UIImage *headImage = [UIImage imageWithContentsOfFile:url];
                        if (headImage) {
                            cell.imageView.image = nil;
                            cell.headImageView.image = [UIImage imageWithContentsOfFile:url];
                        }
                    }else{
                        cell.headImageView.image = nil;
                        cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                    }
                }else{
                    cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                }
                cell.disLabel.text = nil;
                
                switch (getRankListKindFilterType()) {
                    case RLI_RICH_DAY:
                    {
                        cell.signImageView.image = [UIImage imageNamed:@"gold.png"];
                    }
                        break;
                    case RLI_RICH_WEEK:
                    {
                        cell.signImageView.image = [UIImage imageNamed:@"meili.png"];
                    }
                        break;
                    default:
                        cell.signImageView.image = [UIImage imageNamed:@"gold.png"];
                        break;
                }
                
                cell.moneyLabel.text = rlinfo.strmoney;
                
            }
                
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
            break;
        default:
            break;
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch ([indexPath section]) {
        case 0:
        {
            rankListInfo *rlinfo = [[DataControl shareControl].ranklistDS RankListInfoByRow:indexPath.row];
            
            if (rlinfo != nil) {
                
                FirendInfo *finof = [[DataControl shareControl] SearchInfo:rlinfo.strmail];
                
                if (finof != nil) {
                    
                    if ([finof.mail isEqualToString:[DataControl shareControl].personDS.uinfo.mail]) {
                        [[ViewManager sharedViewManager] showView:VI_USERINFO
                                                 withIsRemoveCurV:false
                                                    withDirection:VD_RIGHT];
                    }
                    else
                    {
                    [DataControl shareControl].currentFI = finof;
                    
                    [[ViewManager sharedViewManager] showView:VI_PERSONINFO
                                             withIsRemoveCurV:false
                                                withDirection:VD_RIGHT];
                    }
                }
                
            }
            
            
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -ActionSheet delegate-
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex >= 0 && buttonIndex < 3) {
        setRankListDataFilterType(buttonIndex + 1);
        
        self.mtitle.text = [self getTitleName];
        self.navItem.rightBarButtonItem = nil;
        UIBarButtonItem *nextItem = [self createBarButtonItemWithTitle:[self getFilterName]
                                                                target:self
                                                                action:@selector(nextButtonPressed)];
        self.navItem.rightBarButtonItem = nextItem;
        
        [self SendRanklistRequest];
        
        [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在更新..."
                                                          withView:self.view];
        
        iTimeCount = 0;
        iGameState = VGS_START;
    }
    
}





@end
